<?php
	include("gen.php");
	$cmd = get_datan("cmd");
	switch($cmd){
		//Get a single OPD Record from the ID
		case 1:
			get_opd_case();
			break;

		case 2:
			update_opd_case();
			break;

		case 3:
			delete_opd_case();
			break;

		case 4:
			insert_opd_case();
			break;

		default:
			echo "{";
			echo jsonn("result", 0). ",";
			echo jsons("message", "unknown command");
			echo "}";
	}


	function get_opd_case(){
		include_once("opdClass.php");

		$opd_case_id = get_datan("opd_case_id");
		$new = new opdClass();

		$row = $new->get_opd_case($opd_case_id);

		if(!$row){
			echo "{";
			echo jsonn("result", 0).",";
			echo jsons("message", "No OPD Case Found!");
			echo "}";
			return;
		}
		$row = $new->fetch();

		echo "{";
		echo jsonn("result", 1) . ",";
		echo '"opd_cases":{';
		echo jsonn("opd_case_id", $opd_case_id) . ",";
		echo jsonn("opd_case_name", $row["opd_case_name"]) . ",";
		echo jsonn("opd_case_category", $row["opd_case_category"]);
		echo "}";
		echo "}";
	}

	function update_opd_case(){
		include_once("opdClass.php");

		$new = new opdClass();

		$opd_case_id = get_datan("opd_case_id");
		$opd_case_name = get_data("opd_case_name");
		$opd_case_category = get_datan("opd_case_category");


		if(!$opd_case_id){
			//Error message
			echo '{"result":0, "message":"Updating OPD Case Failed"}';
			return;
		}

		if(!$new->update_opd_case($opd_case_id, $opd_case_name, $opd_case_category)){
			//Error message
			echo '{"result":"0, "message":"Update Failed!"}';
			return;
		}

		echo "{";
		echo jsonn("result", 2) . ",";
		echo '"opd_cases":{';
		echo jsonn("opd_case_id", $opd_case_id) . ",";
		echo jsons("opd_case_name", $opd_case_name) . ",";
		echo jsonn("opd_case_category", $opd_case_category) . ",";
		echo "}";
		echo "}";
	}

	function delete_opd_case(){
		include_once("opdClass.php");

		$new = new opdClass();

		$opd_case_id = get_datan("opd_case_id");

		if(!$opd_case_id){
			//Error Message
			echo '{"result": 0 "message":"Failed to Delete OPD Case"}';
			return;
		}

		$new->delete_opd_case($opd_case_id);

		echo "{";
		echo jsonn("result", 3) . ",";
		echo jsons("message", "Delete Successful!");
		echo "}";
	}

	function insert_opd_case(){
		include_once("opdClass.php");

		$new = new opdClass();
		$opd_case_id = get_datan("opid");
		$opd_case_name = get_data("opcn");
		$opd_case_category = get_datan("opcc");

		
		if(!$new->insert_opd_case($opd_case_name, $opd_case_category)){
			echo '{"result":"0, "message":"Insert Failed!"}';
			return;
		}

		echo "{";
		echo jsonn("result", 4) . ",";
		echo '"opd_cases":{';
		echo jsons("opd_case_name", $opd_case_name) . ",";
		echo jsonn("opd_case_category", $opd_case_category) . ",";
		echo "}";
		echo "}";

	}
?>